
class SchemaBase(object):
    def compactString(self, src):
        parts = src.split('_')
        for i in range(len(parts)):
            parts[i] = parts[i].capitalize()
        newstr = ''.join(parts)
        return newstr
    
    def strictString(self, src):
        if len(src)<1 :
            return ''
        for i in range(len(src)):
            if not src[i].isalpha() and not src[i].isdigit() and not src[i]=='_' :
                return src[:i]
        return src

    def trimEnding(self, src, ending):
        end_len = len(ending)
        if len(src)>end_len and src[-end_len:].lower()==ending.lower():
            return src[:-end_len]
        return src


class SchemaColumn(SchemaBase):
    def __init__(self, field, codeType, codeTypePrefix):
        self.isPK = False
        self.field = field
        self.codeField = codeTypePrefix+self.compactString(field)
        self.codeType = codeType
        
    def getDict(self):
        return {'isPK':self.isPK, 'field':self.field, 'codeField':self.codeField, 'codeType':self.codeType}

class SchemaTable(SchemaBase):
    def __init__(self,rawTableName):
        self.tableName = self.trimEnding(self.strictString(rawTableName),'_xx')
        self.entityName = self.compactString(self.tableName[2:])
        self.macroName = self.tableName.upper()
        self.columns = []
        self.pks = []

    def getDict(self):
        return {'tableName':self.tableName, 'entityName':self.entityName, 'macroName':self.macroName}
